TryAdd Method (T, TimeSpan)

Task Parallel System.Threading

Attempts to add the specified item to the BlockingCollection<(Of <(T>)>).

Namespace:  System.Collections.Concurrent
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
Public Function TryAdd ( _
	item As T, _
	timeout As TimeSpan _
) As Boolean
C#
public bool TryAdd(
	T item,
	TimeSpan timeout
)

Parameters

item
Type: T
The item to be added to the collection.
timeout
Type: System..::.TimeSpan
A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

Return Value

true if the item could be added to the collection within the alloted time; otherwise, false.

Exceptions

ExceptionCondition
System..::.InvalidOperationExceptionThe BlockingCollection<(Of <(T>)>) has been marked as complete with regards to additions.
System..::.ObjectDisposedExceptionThe BlockingCollection<(Of <(T>)>) has been disposed.
System..::.ArgumentOutOfRangeExceptiontimeout is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than MaxValue.
System..::.InvalidOperationExceptionThe underlying collection didn't accept the item.

See Also